#region Using

using System;
using System.Xml;
using System.IO;
using System.Globalization;
using System.Collections.Generic;
using System.Collections.Specialized;
using BlogEngine.Core;
using BlogEngine.Core.Provider.Interfaces;

#endregion

namespace BlogEngine.Core.Provider.XML
{
	/// <summary>
	/// A storage provider for BlogEngine that uses XML files.
	/// <remarks>
	/// To build another provider, you can just copy and modify
	/// this one. Then add it to the web.config's BlogEngine section.
	/// </remarks>
	/// </summary>
	public class CIPhotoEffects : IPhotoEffects
	{

		/// <summary>
		/// Gets a PhotoEffect based on a Guid
		/// </summary>
		/// <param name="id">The PhotoEffects's Guid.</param>
		/// <returns>A matching PhotoEffect</returns>
		public  PhotoEffect Select(Guid id)
		{
			PhotoEffect newPhotoEffect = new PhotoEffect();

			foreach (PhotoEffect photoeffect in PhotoEffect.Items)
				if (photoeffect.Id == id)
					newPhotoEffect = photoeffect;
			
			return newPhotoEffect;
		}

		/// <summary>
		/// Inserts a PhotoEffect
		/// </summary>
		/// <param name="photoeffect">Must be a valid PhotoEffect object.</param>
		public  void Insert(PhotoEffect photoeffect)
		{
			PhotoEffect.Items.Add(photoeffect);
			//PhotoEffect.Items.Sort();
			RewritePhotoEffectFile(PhotoEffect.Items);
		}

		/// <summary>
		/// Updates a PhotoEffect
		/// </summary>
		/// <param name="photoeffect">Must be a valid PhotoEffect object.</param>
		public  void Update(PhotoEffect photoeffect)
		{
			if (!PhotoEffect.Items.Contains(photoeffect))
				PhotoEffect.Items.Add(photoeffect);

			PhotoEffect.Items.Sort();

			RewritePhotoEffectFile(PhotoEffect.Items);
		}

		/// <summary>
		/// Deletes a PhotoEffect
		/// </summary>
		/// <param name="photoeffect">Must be a valid PhotoEffect object.</param>
		public  void Delete(PhotoEffect photoeffect)
		{
			List<PhotoEffect> photoeffects = PhotoEffect.Items;

			if (photoeffects.Contains(photoeffect))
				photoeffects.Remove(photoeffect);

			RewritePhotoEffectFile(photoeffects);
		}

		private void RewritePhotoEffectFile(List<PhotoEffect> photoeffects)
		{
			string fileName = Provider.Folder + "photoeffects.xml";

			if (File.Exists(fileName))
				File.Delete(fileName);

			using (XmlTextWriter writer = new XmlTextWriter(fileName, System.Text.Encoding.UTF8))
			{
				writer.Formatting = Formatting.Indented;
				writer.Indentation = 4;
				writer.WriteStartDocument(true);
				writer.WriteStartElement("photoeffects");

				foreach (PhotoEffect photoeffect in photoeffects)
				{
					writer.WriteStartElement("photoeffect");
					writer.WriteAttributeString("id", photoeffect.Id.ToString());
					writer.WriteAttributeString("title", photoeffect.Title.ToString());
					writer.WriteAttributeString("effect", photoeffect.PhotoEffectName);
					writer.WriteAttributeString("lastmodified", photoeffect.DateModified.AddHours(-BlogSettings.Instance.Timezone).ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture));

					// Parameters
					writer.WriteStartElement("parameters");
					foreach (string key in photoeffect.Parameters.Names)
					{
						writer.WriteStartElement("parameter");
						writer.WriteAttributeString("name", key);
						writer.WriteAttributeString("value", photoeffect.Parameters[key]);
						writer.WriteEndElement();
					}
					writer.WriteEndElement();

					writer.WriteEndElement();
					photoeffect.MarkOld();
				}

				writer.WriteEndElement();
			}
		}

		/// <summary>
		/// Fills an unsorted list of categories.
		/// </summary>
		/// <returns>A List&lt;PhotoEffect&gt; of all PhotoEffects.</returns>
		public  List<PhotoEffect> Fill()
		{
			List<PhotoEffect> photoeffects = new List<PhotoEffect>();
			string fileName = Provider.Folder + "photoeffects.xml";

			if (!File.Exists(fileName))
				return photoeffects;

			XmlDocument doc = new XmlDocument();
			doc.Load(fileName);

			foreach (XmlNode node in doc.SelectNodes("photoeffects/photoeffect"))
			{
				PhotoEffect photoeffect = new PhotoEffect();

				photoeffect.Id = new Guid(node.Attributes["id"].InnerText);
				photoeffect.Title = node.InnerText;
				if (node.Attributes["title"] != null)
					photoeffect.Title = node.Attributes["title"].InnerText;
				if (node.Attributes["effect"] != null)
					photoeffect.PhotoEffectName = node.Attributes["effect"].InnerText;
				if (node.Attributes["lastmodified"] != null)
					photoeffect.DateModified = DateTime.Parse(node.Attributes["lastmodified"].InnerText, CultureInfo.InvariantCulture);

				// Parameters
				foreach (XmlNode node2 in node.SelectNodes("parameters/parameter"))
					photoeffect.Parameters.Set(node2.Attributes["name"].InnerText, node2.Attributes["value"].InnerText);

				photoeffects.Add(photoeffect);
				photoeffect.MarkOld();

			}

			return photoeffects;
		}


	}
}
